<template>
  <el-dialog :title="`${form.spaceId ? '修改' : '添加'}房屋空间`" :visible.sync="show" :close-on-click-modal="false" :show-close="!btnLoading" width="500px" @closed="$refs.form.resetFields()">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item prop="spaceId" class="hide" />
      <el-form-item prop="parentId" label="上级">
        <treeselect v-model="form.parentId" :options="treeOptions" :normalizer="normalizer" show-count placeholder="选择上级" />
      </el-form-item>
      <el-form-item label="编码" prop="spaceCode"><el-input v-model.trim="form.spaceCode" placeholder="请输入编码" :maxlength="80" /></el-form-item>
      <el-form-item label="名称" prop="spaceName"><el-input v-model.trim="form.spaceName" placeholder="请输入名称" :maxlength="255" /></el-form-item>
      <el-form-item label="简称" prop="simpleName"><el-input v-model.trim="form.simpleName" placeholder="请输入简称" :maxlength="80" /></el-form-item>
      <el-form-item label="描述" prop="description"><el-input v-model="form.description" type="textarea" placeholder="请输入内容" :maxlength="255" /></el-form-item>
      <el-form-item label="备注" prop="remark"><el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :maxlength="255" /></el-form-item>
      <el-form-item label="排序" prop="seq"><el-input-number v-model="form.seq" :min="0" :max="1e8" :precision="0" controls-position="right" /></el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" :loading="btnLoading" @click="btnSubmit">确 定</el-button>
      <el-button :disabled="btnLoading" @click="show = false">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
// 添加或修改房屋空间
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { nameRule } from '@/utils/validate'
import { editFormMixin } from '@/utils/mixin'
import { addRoomspace, setRoomspace } from '@/api/checkScore/cqEval'
export default {
  components: { Treeselect },
  mixins: [editFormMixin],
  data() {
    return {
      primaryKey: 'spaceId',
      addApi: addRoomspace,
      updateApi: setRoomspace,
      form: {
        parentId: '',
        spaceId: '',
        spaceCode: '',
        spaceName: '',
        simpleName: '',
        description: '',
        remark: '',
        seq: 0
      },
      rules: {
        spaceCode: [{ required: true, message: '请输入编码' }, nameRule],
        spaceName: [{ required: true, message: '请输入名称' }, nameRule]
      },
      treeOptions: []
    }
  },
  methods: {
    opened(item, { treeData }) {
      this.treeOptions = [{ spaceId: '', spaceName: '主类目', children: treeData }]
    },
    normalizer(node) {
      if (!node.children?.length) delete node.children
      return { id: node.spaceId, label: node.spaceName, children: node.children }
    }
  }
}
</script>
